/*
 * Copyright 1998-2011 by SLIB,
 * 70 rue Villette, 69003 Lyon, France
 * All rights reserved.
 *
 * This software is the confidential and proprietary information
 * of SLIB. (&quot;Confidential Information&quot;).  You
 * shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with SLIB.
 */
package com.petot.claude.gwt.example.client.place;

import java.util.HashMap;
import java.util.Map;

import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceTokenizer;
import com.google.gwt.place.shared.Prefix;

/**
 * <code>NavigationPlace</code>
 * Une {@link Place} associé à la navigation
 *
 * @version $Revision: 6 $ $Date: 2012-02-22 10:46:26 +0000 (Wed, 22 Feb 2012) $
 * @author petotcl, $Author: claude.petot@gmail.com $
 */
public class NavigationPlace extends Place {

    public static final String TOKEN = "nav";

    public enum Navigation /*implements InternationalEnum*/ {
        LOGIN, //
        EXPOSITION_SCENARIOS, //
        POSITION_DETAILS, //
        CLIENT_SETUP_CURRENT, //
        CLIENT_SETUP_PENDING, //
        RISK_ALERTS, //
        SYSTEM_ALERTS, //
        FLOW_MANAGEMENT,  //
        PARAMETERS, //
        PRICES; //

        //        @Deprecated
        //        EXAMPLE;

        public static Navigation[] getAllPages() {
            Navigation[] lAllValues = values();
            Navigation[] lPages = new Navigation[ lAllValues.length - 1 ];
            for ( int i = 1 ; i < lAllValues.length ; i++ ) {
                lPages[ i - 1 ] = lAllValues[ i ];
            }
            return lPages;
        }
    }

    /**
     * Tokenizer.
     */
    @Prefix( NavigationPlace.TOKEN )
    public static class Tokenizer implements PlaceTokenizer<NavigationPlace> {

        public Tokenizer() {
        }

        @Override
        public NavigationPlace getPlace( final String token ) {
            return new NavigationPlace( Navigation.valueOf( token ) );
        }

        @Override
        public String getToken( final NavigationPlace place ) {
            return place.getNavigation().name();
        }
    }

    private final Navigation navigation;

    private static Map<Navigation,NavigationPlace> placesCache = new HashMap<NavigationPlace.Navigation,NavigationPlace>();

    public static NavigationPlace get( final Navigation pNavigation ) {
        if ( placesCache.get( pNavigation ) == null ) {
            NavigationPlace lPlace = new NavigationPlace( pNavigation );
            placesCache.put( pNavigation, lPlace );
        }
        return placesCache.get( pNavigation );
    }

    protected NavigationPlace( final Navigation pNavigation ) {
        this.navigation = pNavigation;
    }

    public Navigation getNavigation() {
        return this.navigation;
    }

    @Override
    public boolean equals( final Object obj ) {
        if ( this == obj ) {
            return true;
        }
        if ( obj == null ) {
            return false;
        }
        if ( getClass() != obj.getClass() ) {
            return false;
        }
        NavigationPlace other = (NavigationPlace)obj;
        if ( this.navigation != other.navigation ) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ( ( this.navigation == null ) ? 0 : this.navigation.hashCode() );
        return result;
    }

    @Override
    public String toString() {
        return "NavigationPlace [" + this.navigation + "]";
    }
}
